/*Copyright (C) 2013-2021 LZE*/
#include<bits/stdc++.h>
#define INF 0x7fffffff
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=500010;
int n,m,s,u[N],v[N],w[N],dis[N];
bool check;
inline int rd() {
	int x=0;bool ntv=false;char c=getchar();
	while((c<'0'||c>'9')&&c!='-')c=getchar();if(c=='-')ntv=true;else x=(c^48);c=getchar();
	while(c>='0'&&c<='9') {x=x*10+(c^48);c=getchar();}return ntv?-x:x;
}
int main() {
	n=rd();m=rd();s=rd();
	for(int i=1;i<=m;i++)cin>>u[i]>>v[i]>>w[i];
	for(int i=1;i<=n;i++)dis[i]=INF;
	dis[s]=0;
	for(int i=1;true;i++){
		check=false;
		for(int j=1;j<=m;j++){
			if(dis[u[j]]!=INF&&dis[v[j]]>dis[u[j]]+w[j]){
				dis[v[j]]=dis[u[j]]+w[j];
				check=true;
			}
		}
		if(!check)break;
		if(i==n){
			cout<<-1;
			return 0;
		}
	}
	for(int i=1;i<=n;i++){
		cout<<dis[i]<<" ";
	}
	return 0;
}
